var tuiwa = require("../../tuiwa.js");
var animation; // 动画实体
var ani_index = 0; // 动画执行次数index（当前执行了多少次）
var aniIntervalId = -1; // 动画定时任务id，通过setInterval来达到无限缩放，记录id，用于结束定时任务

Page({
  data: {
    // filePath: 'https://fcdn.qiushilaile.com/adx/app-101610706237575.png',
    showScore: 0, // 抢到金额
    // is_true: 1,
  },
  onReady() {
    let that = this
    ani_index = 0;
    aniIntervalId = -1;
    this.data.animation = '';
    animation = tt.createAnimation({
      duration: 500,
      timingFunction: 'linear', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
      delay: 0, // 0延迟
      transformOrigin: '50% 50% 0'
    })
    // console.log(this)
    setTimeout(() => {
      this.startAnimationInterval()
    }, 100)
  },
  aniScale: function (n) { // 缩放动画
    if (n % 2 == 0) {
      animation.scale(1.15).step();
    } else {
      animation.scale(1.0).step();
    }
    this.setData({
      animation: animation.export()
    });
  },
  startAnimationInterval: function () { // 开始缩放
    var that = this;
    that.aniScale(++ani_index); // 进行一次缩放
    aniIntervalId = setInterval(function () {
      that.aniScale(++ani_index);
    }, 800); // 每间隔0.5s进行一次缩放
  },
  onLoad: function (query) {
    var that = this
    //获取用户设备信息，屏幕宽度
    tt.getSystemInfo({
      success: res => {
        console.log(res, 'getSystemInfo')
        that.setData({
          screenWidth: res.screenWidth,
          screenHeight: res.screenHeight,
          pixelRatio: res.pixelRatio,
        })
        console.log(that.data.pixelRatio, that.data.screenWidth, that.data.screenHeight)
      }
    })
    tt.getImageInfo({
      src: 'https://fcdn.qiushilaile.com/adx/app-101610706237575.png',
      success: function (res) {
        that.setData({
          shareImgSrc: res.path,
          shareWidth: res.width,
          shareHeight: res.height,
        });
        // console.log("111", that.data.shareImgSrc, that.data.shareWidth, that.data.shareHeight)
      }
    })
    tt.getImageInfo({
      src: 'https://fcdn.qiushilaile.com/adx/app-68841610539842230.png',
      success: function (res) {
        that.setData({
          shareImgSrc1: res.path,
          shareWidth1: res.width,
          shareHeight1: res.height,
        });
        // console.log("111", that.data.shareImgSrc1, that.data.shareWidth1, that.data.shareHeight1)
      }
    })
    tt.getImageInfo({
      src: 'https://fcdn.qiushilaile.com/adx/app-49601610539855858.png',
      success: function (res) {
        that.setData({
          shareImgSrc2: res.path,
          shareWidth2: res.width,
          shareHeight2: res.height,
        });
      }
    })
    this.setData({
      showScore: query.showScore
    })
    console.log(this.data.showScore)
    tuiwa.globalData.currentPage = this
  },
  //获取临时路径
  getTempFilePath: function () {
    let that = this
    console.log('111')
    tt.canvasToTempFilePath({
      canvasId: 'share',
      destWidth: that.data.screenWidth * that.data.pixelRatio,
      destHeight: that.data.screenHeight * that.data.pixelRatio,
      success: (res) => {
        console.log(res)
        that.setData({
          shareTempFilePath: res.tempFilePath
        })
        tt.saveImageToPhotosAlbum({
          filePath: that.data.shareTempFilePath,
          success: (res) => {
            console.log(res)
            tt.showToast({ title: "成功保存到本地相册" });
          },
          fail: (err) => {
            console.log(err)
          }
        })
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },
  back() {
    tt.reLaunch({
      url: '../../pages/index/index' // 指定页面的url
    });
  },
  again: function () {
    tt.redirectTo({
      url: '../../pages/packet-rain/packet-rain'
    });
  },
  //保存至相册
  save: function () {
    console.log(this.data.is_true)
    let _this = this
    if (_this.data.is_true != 1) {
      console.log(this.data.is_true)
      tt.showModal({
        title: '温馨提示',
        content: '点击确定前往观看视频',
        cancelText: '取消',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定');
            tuiwa.playAd();
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      });
    } else {
      console.log(this.data.is_true, '111')
      //绘制
      let that = this;
      let shareWidth = this.data.shareWidth
      let shareWidth1 = this.data.shareWidth1
      let shareWidth2 = this.data.shareWidth2
      let shareHeight = this.data.shareHeight
      let shareHeight1 = this.data.shareHeight1
      let shareHeight2 = this.data.shareHeight2
      let shareImgSrc = this.data.shareImgSrc
      let shareImgSrc1 = this.data.shareImgSrc1
      let shareImgSrc2 = this.data.shareImgSrc2
      let showScore = this.data.showScore
      let context = tt.createCanvasContext('share')
      // 图片
      context.drawImage(shareImgSrc, 0, 0, shareWidth, shareHeight)
      context.drawImage(shareImgSrc1, 40, 200, shareWidth1 / 2.7, shareHeight1 / 2.7)
      context.drawImage(shareImgSrc2, 40, 320, shareWidth2 / 2.7, shareHeight2 / 2.7)
      // 数字
      context.setFontSize(60)
      context.setTextAlign('center')
      context.setFillStyle('#FFEC5D')
      context.setShadow(5, 5, 10, 'red')
      context.fillText(showScore, 240, 260, 115)
      // 文字
      context.setFontSize(20)
      context.font = 'bold 20px Arial, sans-serif'
      context.fillText('新年发大财', 220, 460, 115)
      context.fillText('好运不间断', 220, 480, 115)
      context.fillText('升职再加薪', 220, 500, 115)
      context.fillText('2021牛气冲天', 220, 520, 115)
      // 生成
      context.draw(true, this.getTempFilePath)
      console.log(context)
    }
  },
  closeAdFunction: function () {
    console.log("看完了");
    this.setData({
      is_true: 1
    })
    //绘制
    var that = this;
    var shareWidth = this.data.shareWidth
    var shareWidth1 = this.data.shareWidth1
    var shareWidth2 = this.data.shareWidth2
    var shareHeight = this.data.shareHeight
    var shareHeight1 = this.data.shareHeight1
    var shareHeight2 = this.data.shareHeight2
    var shareImgSrc = this.data.shareImgSrc
    var shareImgSrc1 = this.data.shareImgSrc1
    var shareImgSrc2 = this.data.shareImgSrc2
    var showScore = this.data.showScore
    var context = tt.createCanvasContext('share')
    // 图片
    context.drawImage(shareImgSrc, 0, 0, shareWidth, shareHeight)
    context.drawImage(shareImgSrc1, 40, 200, shareWidth1 / 2.7, shareHeight1 / 2.7)
    context.drawImage(shareImgSrc2, 40, 320, shareWidth2 / 2.7, shareHeight2 / 2.7)
    // 数字
    context.setFontSize(60)
    context.setTextAlign('center')
    context.setFillStyle('#FFEC5D')
    context.setShadow(5, 5, 10, 'red')
    context.font = 'bold 60px sans-serif';
    context.fillText(showScore, 240, 260, 115)
    // 文字
    context.setFontSize(20)
    context.fillText('新年发大财', 220, 460, 115)
    context.fillText('好运不间断', 220, 480, 115)
    context.fillText('升职再加薪', 220, 500, 115)
    context.fillText('2021牛气冲天', 220, 520, 115)
    // 生成
    context.draw(true, this.getTempFilePath)
  },
  cancelAdFunction: function () {
    console.log("没看完广告");
    tt.showModal({
      title: "温馨提示",
      content: "要观看完整视频才能进入哦~",
      showCancel: !1
    })
  },
})
